package com.myecl.poker.view;


import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;


import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

import com.myecl.poker.communication.ClientConnector;
import com.myecl.poker.communication.ViewData;
import com.myecl.poker.rules.Card;
import com.myecl.poker.rules.enums.Rank;
import com.myecl.poker.rules.enums.Suit;



 
public class View {	
	
	private final JFrame frame;
//	private final JPanel panelOuter = new JPanel();
//	private final CardPanel panelComCards = new CardPanel();
//	private final CardPanel panelOwnCards = new CardPanel();
//	private final JPanel buttons = new JPanel();
//	
//	private final BoxLayout layoutOuter = new BoxLayout(panelOuter, BoxLayout.Y_AXIS );
//	private final BoxLayout layoutComCards = new BoxLayout(panelComCards, BoxLayout.X_AXIS );
//	private final BoxLayout layoutOwnCards = new BoxLayout(panelOwnCards, BoxLayout.X_AXIS );
//	private final BoxLayout layoutButtons = new BoxLayout(buttons, BoxLayout.X_AXIS );

	
    public View(String name, ClientConnector connector) {
    	frame = new JFrame(name);  
    	initGUI(connector);
    }
    
    private void initGUI(ClientConnector connector) {
    	JFrame frame = new JFrame("Laying Out Components Using Absolute Coordinates");
    	  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	  JLayeredPane panel = new JLayeredPane();
    	  panel.setSize(1300, 900);
    	  BufferedImage image = null;
      	try {  
      		JLabel label = new JLabel();
      		
      		InputStream input = View.class.getResourceAsStream("green.jpg");
      		image =  ImageIO.read(input);
      		label.setIcon(new ImageIcon(image));
      		label.setBounds(0, 0, 1224,792);
      		panel.add(label, 1);
      		
           } catch (IOException ex) {
                ex.printStackTrace();
           }
      	CardLabel card = new CardLabel(new Card(Suit.CLUBS, Rank.FOUR));
      		panel.add(card, 0);
      		card.setBounds(200, 200, card.getSize().width, card.getSize().height);
    	  frame.add(panel);
    	  frame.setSize(1300,900);
    	  frame.setVisible(true);

    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
//    	frame.add(panelOuter);
//    	
//        panelOuter.setLayout(layoutOuter);
//        panelComCards.setLayout(layoutComCards);
//        panelOwnCards.setLayout(layoutOwnCards);
//        buttons.setLayout(layoutButtons);
//        
//        panelOuter.add(Box.createRigidArea(new Dimension(0,400)));
//    	panelOuter.add(panelComCards);
//    	panelOuter.add(Box.createRigidArea(new Dimension(0,60)));
//    	panelOuter.add(panelOwnCards);
//    	panelOuter.add(Box.createRigidArea(new Dimension(0,40)));
//    	final JFormattedTextField raiseField = InterfaceElements.getInputField();
//    	panelOuter.add(raiseField);
//        panelOuter.add(buttons);
//      
//        buttons.add(InterfaceElements.getFoldButton(connector));
//        buttons.add(InterfaceElements.getCheckButton(connector));
//        buttons.add(InterfaceElements.getCallButton(connector)); 
//        buttons.add(InterfaceElements.getRaiseButton(connector, raiseField));
//
//        frame.setSize(1000, 800);
//        frame.setVisible(true);
    }
//    
    public void update(ViewData viewData) {
//    	panelComCards.setCardLabels(getCardLabels(viewData.getComCards(), 5));
//    	panelOwnCards.setCardLabels(getCardLabels(viewData.getOwnCards(), 2));
//    	panelComCards.revalidate();
//    	panelOwnCards.revalidate();
    }
    
    private List<CardLabel> getCardLabels(List<Card> cards, int number) {
    	List<CardLabel> result = new LinkedList<CardLabel>();
    	for(Card c: cards) {
    		result.add(new CardLabel(c));
    	}
    	int i = cards.size();
    	while (i < number) {
    		result.add(new CardLabel((Card) null));
    		i++;
    	}
    	return result;
    }
  
    
    
   public JFrame getFrame() {
	   return frame;
   }


  
}






